/**
*栏目
*/
var EditArticle = {},tag = 100,mtag = 1000;
var 	sourcekdialog;

var play = {
	ip:{
		rtsp:'rtsp://10.62.48.9/',
		http:'http://10.62.48.9//'
	},
	playPath:"http://10.62.48.9/emsp/apps/sCity/common/VODLauncher.html"
};

EditArticle.source = function(type,ft,id){	
	jQuery("#start").val(0);
	jQuery("#limit").val(9);
	jQuery("#type").val(type);

	EditArticle.createDialog(ft,id);
	
	jQuery("#source_form").ajaxSubmit({
		url:'/CMS/main/resouceList',
		type:'post',
		dataType:'json',
		success:function(data){
			page(data.count,jQuery("#limit").val(),1,"pagePanel","gotoPageSource",true,null,null,false);
			//加载添加时间
			var lines = '<option value="">全部</option>';
			jQuery.each(data.addTime,function(j,m){
				if(m != null)
					lines += '<option value="'+m+'">'+m+'</option>';
			});
			
			jQuery("#faddTimeQuery").html(lines);
			
			bindGridSource(data);
		},	
		error:function(msg){
			jQuery.growlUI('数据加载出错！');
		}
	});
};

EditArticle.selectAddTimeQuery = function(type){
	jQuery("#start").val(0);
	
	var datas = {'faddtime':jQuery("#faddTimeQuery").val()==""?null:jQuery("#faddTimeQuery").val(),'fname':jQuery("#searchSourceName").val()==""?null:jQuery("#searchSourceName").val()};
	
	jQuery("#source_form").ajaxSubmit({
		url:'/CMS/main/resouceList',
		type:'post',
		dataType:'json',
		data:datas,
		success:function(data){
			page(data.count,jQuery("#limit").val(),1,"pagePanel","gotoPageSource",true,null,null,false);
			bindGridSource(data);
		},
		error:function(msg){
			jQuery.growlUI('数据加载出错！');
		}
	});
};
//创建弹出层
EditArticle.createDialog = function(ftitle,id){
		sourcekdialog = KindEditor.dialog({
			width : 700,
			height: 600,
			title : ftitle,
			body : '<div style="overflow:auto;overflow-y:auto;overflow-x:hidden;"><div id="time_source"><span style="float:left;">添加时间：</span><select id="faddTimeQuery"  style="width:180px;font-size:13px;"></select><span><span style="float:left;">资源名称：</span><input type="text" id="searchSourceName" /></span><input type="button" value="    搜   索     " onclick="EditArticle.selectAddTimeQuery()"/></div><br><div id="source_div" style="padding:10px;"></div></div>',
			shadowMode:true,
			closeBtn : {
				name : '关闭',
				click : function(e) {
					sourcekdialog.remove();
				}
			},
			yesBtn : {
				name : '确定',
				click : function(e) {
					var resid = jQuery("input[name='sourceId']:checked").val();
					if(resid != undefined){
						
							var _src = jQuery("#fresourceurl" + resid).attr("src");
							jQuery("#ico_prew" + id).attr("src", _src);
							jQuery("#ico_prew" + id).show();
							jQuery("#img_prew_show" + id).show();
							if(id == 1)
								jQuery("#ico_prew_url").val(_src);
							if(id == 2)
								jQuery("#img_remark").val(_src);
							if(id == 3){	
								var imgs = jQuery("#fresourceurl" + resid);
								jQuery("#video_remark").val(imgs.attr('dataUrl'));
								jQuery("#sendMode").val(imgs.attr('sendMode'));
								jQuery("#mediatype").val(imgs.attr('mediatype'));
								if(imgs.attr('duration') == '' || undefined)
									jQuery("#duration").val(0);
								else
									jQuery("#duration").val(imgs.attr('duration'));
								
								jQuery("#videoTitle").val(jQuery("#title_" + resid).html());
								console.debug(jQuery("#video_remark").val());
							}
								
						}
						sourcekdialog.remove();
				}
			},
			noBtn : {
				name : '取消',
				click : function(e) {
					sourcekdialog.remove();
				}
			}
		});
};

EditArticle.addColImg = function(){
	//
	var checkType = jQuery("input[type='checkbox'][name='linkType']:checked").val();
	
	var imgSrc = jQuery("#img_remark").val(),
		remark = checkType == 'video'?jQuery("#video_remark").val():jQuery("#text_link").val(),
		url = '';
	if(remark != ''){
		if(checkType == 'video'){
			url = play.playPath + "?";
			if(jQuery("#mediatype").val() == "TS"||"ts"){
				url += "&type=VOD";
			}else{
				url += "&type=DVB";
			}
			url += "&title=" + jQuery("#videoTitle").val();
			url += "&playUrl=" + play.ip.rtsp + remark;
			url += "&duration=" + jQuery("#duration").val();
			jQuery("#video_remark").val('');
			jQuery("#img_prew_show3").hide();
		}else{
			url = remark;
			jQuery("#text_link").val('');
		}
	}
	if(imgSrc != ''){
		jQuery("#col_img").append('<div id="l_i_' + tag + '" class="text_box" onmouseover="imgLink.showLayer(1,' + tag + ');" onmouseout="imgLink.showLayer(2,' + tag + ');">\
							<img id="img_src_Source_'+tag+'" src="' + imgSrc + '" width="180px" height="120px"/>\
							<div class="text_t"></div>\
							<div id="div_url_Source_'+tag+'" class="text_t_show">' + url + '</div>\
							<div id="text_bg' + tag + '" class="text_bg" onmouseout="imgLink.showLayer(2,' + tag + ');"></div>\
							<div id="text_bg_t' + tag + '" class="text_bg_t">\
								<a href="javascript:void(0);" style="color:#ffffff;" title="删除图片" onclick="imgLink.remove('+tag+');">删除</a>\
								<a href="javascript:void(0);" style="color:#ffffff;" title="往前移动" onclick="moveDivPannel.before(' +tag+ ');">向前</a>\
								<a href="javascript:void(0);" style="color:#ffffff;" title="往后移动" onclick="moveDivPannel.after(' +tag+ ');">向后</a>\
							</div>\
							</div>');
		jQuery("#img_remark").val('');
		jQuery("#img_prew_show2").hide();
		
		tag ++;
	}
};

EditArticle.addMarquee = function(id){
	var text = jQuery("#marquee_text" + id).val();
	if(text != ''){
		text = text.replace(/\n/g,'');
		text = text.replace(/\r/g,'');
		text = text.replace(/"/g,'“');
		text = text.replace(/'/g,"‘");
		jQuery("#marquee_arr" + id).append('<span class="marquee_t" id="marquee_t_' + id + mtag +'"><img src="/CMS/images/tree/icon_tip.gif" /><span class="marquee_text">' + text + '</span><img src="/CMS/images/tree/icon_cancel.gif" title="删除" onclick="cancerMar(' + id + ',' + mtag + ')" style="float:right;margin-right:10px;cursor:pointer;"/></span>');
		jQuery("#marquee_text" + id).val('');
	}
	mtag ++;
};

function gotoPageSource(start){
	jQuery("#start").val(start);
	var datas = {'faddtime':jQuery("#faddTimeQuery").val()==""?null:jQuery("#faddTimeQuery").val(),'fname':jQuery("#searchSourceName").val()==""?null:jQuery("#searchSourceName").val()};

	jQuery("#source_form").ajaxSubmit({
		url:'/CMS/main/resouceList',
		type:'post',
		dataType:'json',
		data:datas,
		success:function(data){
			bindGridSource(data);
		},
		error:function(msg){
			jQuery.growlUI('数据加载出错！');
		}
	});
}
function bindGridSource(data){
	jQuery("#totalCount").html('总共：'+data.count+'条');
	
	var sourType = jQuery("#type").val();
	
	var html = '<table  width="100%" border="0">';
	if(data.source != ''){
		var tem = 0;
		var wH,hH,zoom;
		
		jQuery.each(data.source,function(i,u){
			zoom = Math.max( Math.max(u.fwidth/140,1) , Math.max(u.fheight/100,1) );
			wH   = u.fwidth / zoom;
			hH   = u.fheight / zoom;
			
			var line = '';
			if(tem == 0){
				line = '<tr style="height:130px;">';
			}
				line +='<td width="33%"  style="text-align:center;"><img title="' +u.fname+ '" id="fresourceurl'+u.id+'" ';
			if(sourType == 1){
				line += 'src="' + u.fresourceurl + '" ';
			}else if(sourType == 2){
				line += 'src="' + u.fbillurl + '" dataUrl = "' + u.fresourceurl + '" sendMode="'+ u.fdefinition +'" mediatype="'+ u.fmediatype +'" duration="'+ u.fduration +'"';
			}	
				line += ' onclick="document.getElementById(\'sourceId'+u.id+'\').checked=true;"  onerror="this.src = \'/CMS/images/imgprew.png\'" style="width:' +  (wH > 0?wH:100) + 'px;height:' + (hH > 0?hH:100) + 'px;cursor:pointer;"/>'
						+'<br><span  style="font-size:14px;width:150px;color:#688187;word-break:break-all;overflow: hidden;white-space: nowrap; text-overflow: ellipsis;display:inline-block;" id="title_'+ u.id +'">'+u.fname+'</span>'
						+'<br><input type="radio" style="margin-left:30%;" id="sourceId'+u.id+'" name="sourceId" value="'+u.id+'" /></td>';
				tem ++;	
				
			if(tem == 3){
				line += '</tr>';
				tem = 0;
			}	
			html += line;	
		});
		
	}else{
		html += '<tr><td colspan="3" style="text-align:center;">无数据</td></tr>';
	}
	html += '</table>';
	var htmlStr =  html + jQuery("#source_div_foot").html();
//	jQuery.growlUI(htmlStr);
	if(jQuery("#type").val() == 1){///图片加载器
		var imgtab = '<div style="width:100%;height:auto !important;min-height:30px;border:1px solid #ffffff;">'
			+'<div id="div_tab1" onclick="DivTab.clickOn(1,2)" class="div_li_style_on">图片库</div><div id="div_tab2" onclick="DivTab.clickOn(2,1)" class="div_li_style">上传图片</div>'
			+'<div id="div_tab_sorce1" style="width:100%;border:1px solid #ffffff;border-top-color:#ccc;margin-top:20px;">'+htmlStr+'</div>'
			+'<div id="div_tab_sorce2" style="width:100%;border:1px solid #ffffff;border-top-color:#ccc;margin-top:20px;display:none;">'
			+'<div style="margin-top:20px;"><form id="img_upload_form" action="article_uploadimage" method="post" enctype="multipart/form-data">'
			+'选择图片：<input type="file" name="file" style="font-size:13px;" onchange ="ImgUpload.validataFileType(this)"/>'
			+'<input type="button" value="上传" id="uploadimg" onclick="ImgUpload.imgupload()" style="font-size:13px;width:50px;"/>'
			+'(支持格式：.gif.jpg.jpeg.png)</form></div>'
			+'<div style="clear:both;"></div></div>'
			+'</div>';
		jQuery("#source_div").html(imgtab);
	}else{
		jQuery("#source_div").html(htmlStr);
	}
}
var ImgUpload ={
		//上传图片验证
		validataFileType:function(obj){
			var img = obj.value;
			var suffix =img.substring(img.lastIndexOf("."),img.length);
			var rep = new RegExp('(.gif|.GIF|.jpeg|.JPEG|.jpg|.JPG|.png|.PNG]jQuery)');
			if(!rep.test(suffix)){
				jQuery.growlUI('图片格式不正确');
				jQuery("#uploadimg").attr('disabled','disabled');
			}else{
				jQuery("#uploadimg").attr('disabled',false);
			}
		},
		//上传
		imgupload:function(){
			if(jQuery("input[type=file][name='file']").val() != ''){
				jQuery("#img_upload_form").ajaxSubmit({
					url:'/CMS/main/article_uploadimage',
					type:'post',
					dataType:'json',
					success:function(data){
						if(data > 0){
							jQuery.growlUI('上传成功');
							sourcekdialog.remove();
							EditArticle.source(1,'图片库');
						}else{
							jQuery.growlUI(data);
						}
					},
					error:function(msg){
						jQuery.growlUI('上传出错');
					}
				});
			}else{
				jQuery.growlUI('请选择图片');
			}
		}
		
};
var DivTab = {
		clickOn:function(id,di){
			jQuery("#div_tab"+id).removeClass();
			jQuery("#div_tab"+id).addClass('div_li_style_on');
			jQuery("#div_tab_sorce"+id).show();
			jQuery("#div_tab"+di).removeClass();
			jQuery("#div_tab"+di).addClass('div_li_style');
			jQuery("#div_tab_sorce"+di).hide();
		}	
};

var imgLink = {
	showLayer:function(type,id){
		if(type == 1){
			jQuery("#text_bg" + id).show();
			jQuery("#text_bg_t" + id).show();
		}else{
			jQuery("#text_bg" + id).hide();
			jQuery("#text_bg_t" + id).hide();
		}
	},
	remove:function(id){
		jQuery("#l_i_" + id).remove();
	},
	gennerJSON:function(){
		var imgs = "",mars = jQuery("#marquee_arr1").find('.marquee_text'),mess = jQuery("#marquee_arr2").find('.marquee_text');
		
		var imgObj = jQuery(".text_box");
		if(mars.size() > 0){
			var mar_text = "";
			mars.each(function(){
				mar_text += '"' + jQuery(this).html() + '",';
			});
			jQuery("#mar_source").val('{"marquee":[' + mar_text.substring(0,mar_text.length - 1) + '],"name":"marquee"},');
		}
		if(mess.size() > 0){
			var mess_text = "";
			mess.each(function(){
				mess_text += '{"text":"' + jQuery(this).html() + '"},';
			});
			jQuery("#msg_source").val('{"SMS":[' + mess_text.substring(0,mess_text.length - 1) + '],"name":"SMS"},');
		}
		if(imgObj.size() > 0){
			imgs = '{"name":"img","img":[';
			imgObj.each(function(){
				imgs += '{"src":"' + jQuery(this).find('img').attr('src') + '","url":"' + 	jQuery(this).find('.text_t_show').html() + '"},';
			});
			imgs = imgs.substring(0,imgs.length - 1);
			imgs += ']},';
		}
		jQuery("#json_source").val(imgs);
	}
};

var moveDivPannel = {
	after : function(tag){
		if(jQuery("#l_i_" + (tag + 1))){
			var src = jQuery("#img_src_Source_" + tag).attr('src'),
				url = jQuery("#div_url_Source_" + tag).html();
			jQuery("#img_src_Source_" + tag).attr('src',jQuery("#img_src_Source_" + (tag + 1)).attr('src'));
			jQuery("#div_url_Source_" + tag).html(jQuery("#div_url_Source_" + (tag + 1)).html());
			jQuery("#img_src_Source_" + (tag + 1)).attr('src',src);
			jQuery("#div_url_Source_" + (tag + 1)).html(url);
		}
	},
	before : function(tag){
		if(jQuery("#l_i_" + (tag - 1))){
			var src = jQuery("#img_src_Source_" + tag).attr('src'),
			url = jQuery("#div_url_Source_" + tag).html();
			jQuery("#img_src_Source_" + tag).attr('src',jQuery("#img_src_Source_" + (tag - 1)).attr('src'));
			jQuery("#div_url_Source_" + tag).html(jQuery("#div_url_Source_" + (tag - 1)).html());
			jQuery("#img_src_Source_" + (tag - 1)).attr('src',src);
			jQuery("#div_url_Source_" + (tag - 1)).html(url);
		}
	}
};

function cancelIMG(id){
	jQuery("#ico_prew" + id).hide();
	jQuery("#ico_prew_url").val('');
	jQuery("#img_prew_show" + id).hide();
}
function cancerMar(id,obj){
	jQuery("#marquee_t_" + id + obj).remove();
}

function checkLinkT(O){
	jQuery("input[type='checkbox'][name='linkType']").attr('checked',false);
	O.checked = 'checked';
	if(O.value == 'link'){
		jQuery("#iLink").show(100);
		jQuery("#vLink").hide(100);
	}else{
		jQuery("#iLink").hide(100);
		jQuery("#vLink").show(100);
	}
}